﻿using System;
using System.Security.Claims;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Furion;
using Furion.DependencyInjection;
using Furion.EventBus;
using Furion.FriendlyException;
using Magic.Core.Entity;
using Microsoft.AspNetCore.Mvc.Filters;
using Serilog;

namespace Magic.Core;

/// <summary>
/// 全局异常处理
/// </summary>
public class LogExceptionHandler : IGlobalExceptionHandler, ISingleton
{
    private readonly IEventPublisher _eventPublisher;

    public LogExceptionHandler(IEventPublisher eventPublisher)
    {
        _eventPublisher = eventPublisher;
    }

    public async Task OnExceptionAsync(ExceptionContext context)
    {
        var isValicationException = context.Exception is AppFriendlyException { StatusCode: 400 };
        if (!isValicationException)
        {
            var userContext = App.User;
            var className = context.Exception.TargetSite.DeclaringType?.FullName;
            var groupCollection = Regex.Match(className, "<(.*?)>").Groups;
            var methodName = "";
            if (groupCollection.Count > 1)
            {
                methodName = groupCollection[1].Value;
            }
            await _eventPublisher.PublishAsync(new ChannelEventSource("Create:ExLog",
                new SysLogEx
                {
                    Account = userContext?.FindFirstValue(ClaimConst.CLAINM_ACCOUNT),
                    Name = userContext?.FindFirstValue(ClaimConst.CLAINM_NAME),
                    ClassName = className,
                    MethodName = methodName,
                    ExceptionName = context.Exception.Message,
                    ExceptionMsg = context.Exception.Message,
                    ExceptionSource = context.Exception.Source,
                    StackTrace = context.Exception.StackTrace,
                    ParamsObj = context.Exception.TargetSite.GetParameters().ToString(),
                    ExceptionTime = DateTime.Now
                }));
            Log.Error(context.Exception.ToString());
        }

    }
}
